<template>
	<view class="container">
		<!-- 商品图片 -->
		<image 
			:src="testData.imageUrl"
			mode="aspectFit"
			class="product-image"></image>
		<!-- 商品信息 -->
		<text class="product-info">购买 1 件{{testData.name}}</text>
		<!-- 商品价格 -->
		<text class="product-price">{{testData.price}} 元</text>
		<!-- 微信支付按钮 -->
		<button type="primary"
				@click="onWechatPayButtonClicked()"
				class="pay-container" >微信支付</button>
	</view>
</template>

<script>

	
	let tag = 'purchase'
	
	export default {
		data() {
			return {
				testData: ""
			}
		},
		methods: {
			onWechatPayButtonClicked() {
				this.$log.info(tag, "onWechatPayButtonClicked");
				this.createOrder();
			},
			
			createOrder() {
				this.$log.info(tag, "createOrder");
				
				uni.request({
					url: `${this.constant.BASE_URL}/api/order/create4Ma`,
					method:'POST',
					header: {
						'Authorization': this.loginManager.getToken(),
					},
					data: {
						'amount': 1,
						'machineNo': this.constant.MACHINE_NO,
						'productId': this.testData.id,
					},
					success:(res)=> {
						this.$log.info(tag, JSON.stringify(res.data))
						this.$log.info(tag, `createOrder success ${res.data.code}`)
						if (res.data.code == 200) {
							this.$log.info(tag, "创建成功")
							this.payOrder(res.data.orderNo)
						}
					},
					fail:(error) =>{
						console.error(error);
					}
				})
			},
			payOrder(orderNo){
				this.$log.info(tag, `payOrder ${orderNo}`)
				uni.request({
					url: `${this.constant.BASE_URL}/wxpay/notify/pay/${orderNo}`,
					method: 'GET',
					success: (res) => {
						this.$log.info(tag, "sucess")
						this.$log.info(tag, JSON.stringify(res.data))
						if(res.data.code==200){
							uni.showToast({
								icon:'success',
								title: `支付成功${orderNo}`,
								duration:1500
							}),
							uni.navigateTo({
								url: `/pages/order/order?orderNo=${orderNo}`
							})
						}
					},
					fail: (err) => {
						this.$log.error(tag, err.errMsg)
					}
				})
				
			},
			
		},
		
		onLoad(parameters) {
			this.$log.info(tag, `onLoad`)
			let productJSONString = decodeURIComponent(parameters.product)
			let product = JSON.parse(productJSONString)
			this.testData = product
		},
		
		onShow() {
			this.$log.info(tag, `onShow`);
		},
		
		onReady() {
			this.$log.info(tag, `onReady`);
		},
		
		onHide() {
			this.$log.info(tag, `onHide`);
		},
		
		onUnload() {
			this.$log.info(tag, `onUnload`);
		},
	}
</script>

<style>
	.container {
		padding: 10px;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	
	.product-image {
		width: 150px;
		height: 150px;
	}
	
	.product-info {
		margin-top: 10px;
		font-size: 36rpx;
		color: dimgrey;
	}
	
	.product-price {
		margin-top: 10px;
		font-weight: bold;
		font-size: 70rpx;
		color: coral;
	}
	
	.pay-container {
		margin-top: 20px;
		padding: 5px;
		width: 80%;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		border-radius: 10px;
		border-width: 1px;
		border-style: solid;
		border-color: lightgrey;
	}
	
	.pay-container:active {
		opacity: 0.5;
	}
	
	.pay-icon {
		width: 40px;
		height: 40px;
	}
	
	.pay-text {
		margin-left: 10px;
		font-size: 36rpx;
	}
</style>
